package com.hzt.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzt.system.common.Constant;
import com.hzt.system.common.DataGridView;
import com.hzt.system.domain.Menu;
import com.hzt.system.mapper.RoleMapper;
import com.hzt.system.vo.MenuVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.hzt.system.mapper.MenuMapper;
import com.hzt.system.service.MenuService;
import sun.security.jca.ServiceId;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Menu> queryMenuForListByUserId(Integer id) {
        //根据用户id查询角色Id的集合
        List<Integer> roleIds = this.roleMapper.queryRoleIdsByUserId(id);
            if(roleIds!=null&&roleIds.size()>0){
                //根据角色id的集合查询，查询菜单id的集合
                List<Integer> menuIds= this.roleMapper.queryMenuIdsByRoleIds(roleIds);
                if(menuIds!=null&&menuIds.size()>0){
                    QueryWrapper<Menu> qw=new QueryWrapper<>();
                    qw.eq("available", Constant.AVAILABLE_TRUE);
                    qw.and(new Consumer<QueryWrapper<Menu>>() {
                        @Override
                        public void accept(QueryWrapper<Menu> menuQueryWrapper) {
                            menuQueryWrapper.eq("type", Constant.MENU_TYPE_TOP)
                                    .or().eq("type", Constant.MENU_TYPE_LEFT);
                        }
                    });
                    qw.in("id", menuIds); //此处id值菜单表中的菜单id
                    qw.orderByAsc("ordernum");
                    List<Menu> menus = this.menuMapper.selectList(qw);
                    return menus;
                }else {
                    return  new ArrayList<>();
                }
            }else {
                return new ArrayList<>();
            }



    }

    @Override
    public List<Menu> queryAllMenuForList() {
        QueryWrapper<Menu> qw = new QueryWrapper();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        qw.and(new Consumer<QueryWrapper<Menu>>() {
            @Override
            public void accept(QueryWrapper<Menu> menuQueryWrapper) {
                menuQueryWrapper.eq("type", Constant.MENU_TYPE_TOP)
                        .or().eq("type", Constant.MENU_TYPE_LEFT);
            }
        });
        qw.orderByAsc("ordernum");
        return this.menuMapper.selectList(qw);
    }

    @Override
    public DataGridView loadAllMenu(MenuVo menuVo) {
        QueryWrapper<Menu> qw=new QueryWrapper<>();
        qw.eq(menuVo.getAvailable()!=null, "available", menuVo.getAvailable());
        qw.orderByAsc("ordernum");
        List<Menu> menus = this.menuMapper.selectList(qw);

        return new DataGridView(Long.valueOf(menus.size()),menus);
    }

    @Override
    public Integer queryMenuMaxOrderNum() {
        return this.menuMapper.queryMenuMaxOrderNum();
    }

    @Override
    public Menu saveMenu(Menu menu) {
        this.menuMapper.insert(menu);
        return menu;
    }

    @Override
    public Integer queryMenuChildCountById(Integer id) {
        return this.menuMapper.queryMenuChildCountById(id);
    }

    @Override
    public Menu updateMenu(Menu menu) {
        this.menuMapper.updateById(menu);
        return menu;
    }

    @Override
    public List<String> queryPermissionodeByUserId(Integer id) {
        //根据用户id查询角色Id的集合
        List<Integer> roleIds = this.roleMapper.queryRoleIdsByUserId(id);
        if(roleIds!=null&&roleIds.size()>0){
            //根据角色id的集合查询，查询菜单id的集合
            List<Integer> menuIds= this.roleMapper.queryMenuIdsByRoleIds(roleIds);
            if(menuIds!=null&&menuIds.size()>0){
                QueryWrapper<Menu> qw=new QueryWrapper<>();
                qw.eq("available", Constant.AVAILABLE_TRUE);
                qw.eq("type", Constant.MENU_TYPE_PERMISSION);
                qw.in("id", menuIds); //此处id值菜单表中的菜单id
                qw.orderByAsc("ordernum");
                List<Menu> menus = this.menuMapper.selectList(qw);
                List<String> permissions=new ArrayList<>();
                for (Menu menu : menus) {
                    permissions.add(menu.getTypecode());
                }
                return permissions;
            }else {
                return  null;
            }
        }else {
            return null;
        }

    }


}

